package Servlet.Admin;

import Entity.MyTableData;
import Service.Admin.Imp.TitleManagementServiceImp;
import Service.Admin.TitleManagementService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/teacherAssign","/getStudentWithoutTeacher","/adminGetAllTitles"})
public class TitleManegementServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/teacherAssign".equals(url)) {
            teacherAssign(request,response);
        } else if ("/getStudentWithoutTeacher".equals(url)) {
            getStudentWithoutTeacher(request,response);
        } else if ("/adminGetAllTitles".equals(url)) {
            adminGetAllTitles(request,response);
        }
    }

    /**
     * 得到所有未分配指导教师学生的信息
     */
    private void getStudentWithoutTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleManagementService titleManagementService = new TitleManagementServiceImp();
        MyTableData myTableData = titleManagementService.getStudentWithoutTeacher();
        Object json = JSONObject.toJSON(myTableData);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 分配指导教师
     */
    protected void teacherAssign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleManagementService titleManagementService = new TitleManagementServiceImp();
        String tno = request.getParameter("tno");
        String sno = request.getParameter("sno");
        boolean flag = titleManagementService.teacherAssign(tno,sno);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 得到所有课题信息
     */
    private void adminGetAllTitles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleManagementService titleManagementService = new TitleManagementServiceImp();
        MyTableData myTableData = titleManagementService.adminGetAllTitles();
        Object json = JSONObject.toJSON(myTableData);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
